#!/usr/bin/env perl
# -*-mode:cperl; indent-tabs-mode: nil-*-

## Truncate the log files for all the test databases
## NOT a test, just a testing aid

use 5.008003;
use strict;
use warnings;
use Time::HiRes qw/sleep/;

opendir my $dh, '.' or die qq{Could not opendir current directory\n};
while (my $name = readdir($dh)) {

	next if $name !~ /^bucardo_test_database_/;

	my $logfile = "$name/pg.log";

	next if ! -e $logfile or ! -s _;

    open my $fh, '<+', $logfile or die;
    seek $fh, 0, 0;
    truncate $fh, 0;
    close $fh;

	my $pidfile = "$name/postmaster.pid";

	if (-e $pidfile) {
		open my $fh, '<', $pidfile or die qq{Could not open "$pidfile": $!\n};
		my $pid = int <$fh>;
		close $fh or warn qq{Could not close "$pidfile": $!\n};
        kill 1 => $pid;
#		kill 15 => $pid;
        #	kill 3  => $pid;
	}

	#sleep 0.2;

	#unlink $logfile;

	warn "Removed: $logfile\n";

}
closedir $dh or warn qq{Could not closedir current directory\n};
